package cn.edu.gzccc.movie.Controller;

import cn.edu.gzccc.movie.domain.Actor;
import cn.edu.gzccc.movie.domain.Movie;
import cn.edu.gzccc.movie.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping(value = "/movie")
public class MovieController {

    @Autowired
    private MovieService movieService;

    /**
     * 电影列表页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/index/{page}", method = RequestMethod.GET)
    public String index(Model model, @PathVariable Integer page) {
        Page<Movie> list = movieService.findMoiveNoCriteria(page, 10);
        model.addAttribute("list", list);
        return "movie/index";
    }

    /**
     * 电影详情页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public String detail(Model model, @PathVariable Long id) {
        Movie movie = movieService.findOne(id);
        model.addAttribute("info", movie);
        return "movie/detail";
    }

    /**
     * 跳转到新增电影页面
     *
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String toAdd() {
        return "movie/add";
    }

    /**
     * 将新增电影数据保存到数据库并重定向到电影列表页
     *
     * @param movie
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String doAdd(Movie movie, BindingResult bindingResult) {
        movieService.add(movie);
        return "redirect:/movie/index/0";
    }

    /**
     * 跳转到电影编辑页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String toEdit(Model model, @PathVariable Long id) {
        Movie movie = movieService.findOne(id);
        model.addAttribute("info", movie);
        return "movie/edit";
    }

    /**
     * 将修改后的电影数据保存到数据库并重定向到电影详情页面
     *
     * @param movie
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String doEdit(Movie movie, BindingResult bindingResult) {
        movieService.update(movie);
        return "redirect:/movie/detail/" + movieService.getMovieId(movie.getId());
    }

    /**
     * 删除并重定向到电影详情页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/delect/{id}", method = RequestMethod.GET)
    public String delect(Model model, @PathVariable Long id) {
        movieService.delete(id);
        return "redirect:/movie/index/0";
    }

}
